package com.coship.acf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import test.fanxin.clases.CarBean;

public class CacheService {
	private static Logger logger = LoggerFactory.getLogger(CacheService.class);

	private static ThreadLocal<String> userLocal = new ThreadLocal<String>();
	
	public static void set(final String key, final Object value) throws Exception {
		server().set(key, value);
	}
	 
	public static void mset(final List<Object> keyValues) throws Exception {
		server().mset(keyValues);
	}

	public static Object get(final String key) throws Exception {
		return server().get(key);
	}
	
	public static List<Object> mget(final String... keys) throws Exception {
		return server().mget(keys);
	}

	public static void remove(final String key) throws Exception {
		server().remove(key);
	}
	
	private static CacheServer server(){
		return CacheServers.getShardServer();
	}
	
	public static byte[] serialize(Object object) throws Exception {
		if(object == null){
			return null;
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		oos.writeObject(object);
		byte[] bytes = baos.toByteArray();
		return bytes;
	}

	public static Object unserialize(byte[] bytes) throws Exception {
		if(bytes == null){
			return null;
		}	
		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		ObjectInputStream ois = new ObjectInputStream(bais);
		return ois.readObject();
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> describe(Object value) throws Exception {
		Map<String, Object> map = BeanUtils.describe(value);
		map.remove("class");
		return map;
	}
	
	public static void setUser(String userId) {
		userLocal.set(userId);
		CacheServers.setShardToken(userId);
	}

	public static String getUser() {
		return userLocal.get();
	}

	public static boolean isCacheEnable(){
		return CacheConfig.isCacheEnable() && CacheServers.isActive();
	}
	
	public static void log(String message){
		if(CacheConfig.isLogDebug()){			
			logger.debug(message);
		}else if(CacheConfig.isLogInfo()){
			logger.info(message);			
		}
	}

	public static void main(String[] args) {
		System.out.println("is run...");
		/**
		for(String server : new String[]{"192.168.1.105"}){			
			Jedis jedis = new Jedis(server);
			//jedis.set("test", "1234");
			for(byte[] key : jedis.keys("*".getBytes())){
				
			    //System.out.println(key);
				//jedis.del(key);
				System.out.println(new String(key));
			}
		}*/
		
		initTest();
		
		System.out.println("is end...");
	}
	
	
	public static void test(){
		
		try {
			CarBean object = new CarBean();
			
			object.setBrand("brand");
			object.setName("name");
			object.setPrice("5.0");
			/**
			
			byte[]  byteDatea= CacheService.serialize(object);
			
			
			object = (CarBean)CacheService.unserialize(byteDatea);
			
			System.out.println(object.getBrand());
			*/
			
			Map<String, Object>  mapTest = CacheService.describe(object);
			
			System.out.println(mapTest);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	
	}
	
	
private static ScheduledExecutorService heartBeatPool = Executors.newScheduledThreadPool(1);
	
	
	public static void initTest() {
		heartBeatPool.scheduleAtFixedRate(new Runnable() {
			public void run() {
				
				System.out.println(Thread.currentThread().getName() + " is run.......");
				
			}
		}, 1, 1, TimeUnit.SECONDS);
	}
}